<!DOCTYPE html>
<!--主要是给搜索引擎看的.这里即使指定为en,在body中依然可以使用中文-->
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JSON POST 示例</title>
</head>
<body>
<h3>发送 JSON 请求到 Tomcat</h3>
<button onclick="sendJson()">发送</button>

<script>
    function sendJson() {
        const data = {
            name: "张三",
            age: 25
        };

        fetch("/json", {
            method: "POST",
            headers: {
                    "Content-Type": "application/json; charset=GBK"
            },
            body: JSON.stringify(data)
        })
            // .then(response => response.text())
            // .then(result => {
            //     alert("服务器响应：" + result);
            // })

            .then(response => response.arrayBuffer())
            .then(buffer => {
                // 使用 GBK 解码
                const decoder = new TextDecoder("gbk");
                const text = decoder.decode(buffer);
                alert("服务器响应：" + text);
            })

            .catch(error => {
                console.error("请求失败：", error);
            });
    }
</script>
</body>
</html>
